<?php
	class Match extends Model{
		
		private $_MatchId;
		private $_Player1;
		private $_Player2;
		private $_Result;
		private $_Round;
		private $_Tournament;

		function __construct($arrInitialValues = null){
			parent::__construct();

			if (isset($arrInitialValues['match_id']))
				$this->setMatchId($arrInitialValues['match_id']);
			else
				$this->setMatchId(0);

			if (isset($arrInitialValues['player1']))
				$this->setPlayer1($arrInitialValues['player1']);
			else
				$this->setPlayer1(new Player());

			if (isset($arrInitialValues['player2']))
				$this->setPlayer2($arrInitialValues['player2']);
			else
				$this->setPlayer2(new Player());
				
			if (isset($arrInitialValues['result']))
				$this->setResult($arrInitialValues['result']);
			else
				$this->setResult(0);
				
			if (isset($arrInitialValues['round']))
				$this->setRound($arrInitialValues['round']);
			else
				$this->setRound(new Round());
				
			if (isset($arrInitialValues['tournament']))
				$this->setTournament($arrInitialValues['tournament']);
			else
				$this->setTournament(new Tournament());
		}
		
		public function setMatchId($value){
			$this->_MatchId = $value;
		}
		
		public function getMatchId(){
			return $this->_MatchId;
		}
		
		public function setPlayer1($value){
			$this->_Player1 = $value;
		}
		
		public function getPlayer1(){
			return $this->_Player1;
		}

		public function setTournament($value){
			$this->_Tournament = $value;
		}
		
		public function getTournament(){
			return $this->_Tournament;
		}
		
		public function setPlayer2($value){
			$this->_Player2 = $value;
		}
		
		public function getPlayer2(){
			return $this->_Player2;
		}

		public function setResult($value){
			$this->_Result = $value;
		}
		
		public function getResult(){
			return $this->_Result;
		}

		public function getRound(){
			return $this->_Round;
		}
		
		public function setRound($value){
			$this->_Round = $value;
		}

		public function delete(){
			$query = '
				DELETE
				FROM matches
				WHERE match_id = ' . $this->getMatchId();
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}

		public function getMatches(){
			$query = '
				SELECT *
				FROM matches
				WHERE match_id = ' . $this->getMatchId();
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function insert(){
			$query = '
				INSERT INTO matches
				(player1_id, player2_id, result, tournament_id, round_id)
				VALUES
				(' . $this->getPlayer1() . ', ' . $this->getPlayer2() . ', "' . $this->getResult() . '", ' . $this->getTournament() . ', ' . $this->getRound() . ')';
			
			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
		
		public function insertMatch($arrMatch){
			$query = '
				INSERT INTO international_matches
				(player1, player2, player3, player4, result, type, serie_id)
				VALUES
				(' . $arrMatch['players'][1] . ', ' . $arrMatch['players'][2] . ', ' . $arrMatch['players'][3] . ', ' . $arrMatch['players'][4] . ', "' .
				$arrMatch['result'] . '", "' . $arrMatch['type'] . '", ' . $arrMatch['header']['serie_id'] .')';

			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
		
		public function update(){
			$query = '
				UPDATE matches
				SET player1_id = ' . $this->getPlayer1() . ', 
					player2_id = ' . $this->getPlayer2() . ', 
					result = "' . $this->getResult() . '",
					tournament_id = ' . $this->getTournament() . ',
					round_id = ' . $this->getRound() . ' 
				WHERE match_id = ' . $this->getMatchId();
				
			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
	}
?>